﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace WebMvc.Areas.AssetManage.Controllers
{
    public class AmSampleController : MyController
    {
        //
        // GET: /AssetManage/AmSample/
        [MyAttribute(CheckApp = false, CheckUrl = false)]
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        [MyAttribute(CheckApp = false, CheckUrl=false)]
        public string AddSample(string address, string bgbhs, int type, string group)
        {
            YJ.Data.MSSQL.DBHelper db = new YJ.Data.MSSQL.DBHelper(System.Configuration.ConfigurationManager.ConnectionStrings["SampleConnection"].ConnectionString);
            try
            {
                string cloneBgbhs=bgbhs;
                int count = 0;
                string sql =string.Format("select * from OA_SampleView where '{0}' like '%'+bgbh+'%'",bgbhs);
                DataTable dt=db.GetDataTable(sql);
                AssetManage.Data.Model.AmSample sam;
                foreach (DataRow item in dt.Rows)
                {
                    sam=new Data.Business.AmSample().GetByBgbh(item["bgbh"].ToString());
                    if (sam!=null)
                    {
                        sam.Address = address;
                        sam.GroupCode = group;
                        using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope())
                        {
                            db.Execute(string.Format("INSERT INTO [AmSampleInOut]([AmsAampleId],[UseUId],[Address1])VALUES('{0}','u_{1}','{2}')", sam.bgbh, CurrentUser.ID, sam.Address));
                            new Data.Business.AmSample().Update(sam);
                            
                            scope.Complete();
                        }
                        count++;
                        bool isContains1 = bgbhs.IndexOf(sam.bgbh.Trim() + ",", StringComparison.OrdinalIgnoreCase) >= 0;
                        if (isContains1)
                        {
                            bgbhs = bgbhs.Replace(sam.bgbh.Trim() + ",", "");
                        }
                        else
                        {
                            bgbhs = bgbhs.Replace(sam.bgbh.Trim(), "");
                        }
                        continue;
                    }
                    sam = new Data.Model.AmSample();
                    sam.Address = address;
                    sam.bgbh = item["bgbh"].ToString();
                    sam.Type = type;
                    sam.GroupCode = group;
                    sam.rwlx= item["rwlx"].ToString();
                    sam.cydh = item["cydh"].ToString();
                    sam.sjqy_mc = item["sjqy_mc"].ToString();
                    sam.ypmc = item["ypmc"].ToString();
                    sam.yp_ggxh = item["yp_ggxh"].ToString();
                    sam.yp_sl = item["yp_sl"].ToString();
                    sam.yp_ddrq = item["yp_ddrq"].ToString().ToDateTime();
                    sam.wtdw = item["wtdw"].ToString();
                    sam.scdw = item["scdw"].ToString();
                    sam.yp_bzq = item["yp_bzq"].ToString();
                    sam.panding = item["panding"].ToString();
                    sam.pz_time = item["pz_time"].ToString().ToDateTime();
                    sam.pz_bz = item["pz_bz"].ToString().ToInt32();
                    sam.yp_scrq = item["yp_scrq"].ToString();
                    sam.ypzt = item["ypzt"].ToString();
                    sam.yp_byl = item["yp_byl"].ToString();
                    if (new Data.Business.AmSample().Add(sam)>0)
                    {
                        count++;
                        bool isContains = bgbhs.IndexOf(sam.bgbh.Trim() + ",", StringComparison.OrdinalIgnoreCase) >= 0;
                        if (isContains)
                        {
                            bgbhs = bgbhs.Replace(sam.bgbh.Trim() + ",", "");
                        }
                        else
                        {
                            bgbhs = bgbhs.Replace(sam.bgbh.Trim(), "");
                        }
                    }
                    

                    //samList.Add(sam);

                }
                YJ.Platform.Log.Add("样品扫码入库",string.Format("操作员：{0}\n原始样品码:{1}\n成功添加条数：{2}\n添加失败样品码信息：{3}",CurrentUserName,cloneBgbhs,count,bgbhs), YJ.Platform.Log.Types.信息管理);
                return "{\"data\":" + count + ",\"status\":1,\"msg\":\""+ bgbhs+ "\"}";
            }
            catch (Exception e)
            {

                return "{\"status\":0,\"msg\":\"" + e.Message + "\"}";
            }
        }
    }
}
